package de.webtech.faceplace.pages;

import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.hibernate.annotations.CommitAfter;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.services.Request;
import org.apache.tapestry5.services.Response;
import org.hibernate.Session;

import de.webtech.faceplace.entities.Comments;
import de.webtech.faceplace.entities.Messages;
import de.webtech.faceplace.entities.Person;

public class Profile {

	@Property
	private Messages currentMessage;

	@Property
	private Comments currentComment;

	@Property
	private String comment;

	@Inject
	private Request request;

	@Inject
	private Response response;

	@Inject
	private Session session;

	public Person getPerson() {
		Long id = (long) 0;
		try {
			id = Long.parseLong(request.getParameter("uid"));
		} catch (Exception e) {
		}
		Person p = null;
		if (id != 0) {
			for (Object pp : session.createCriteria(Person.class).list()) {
				Person ppp = (Person) pp;
				if (ppp.id == id) {
					return ppp;
				}
			}

		}
		return (Person) request.getSession(true).getAttribute("user");

	}

	public String getFirstName() {
		return getPerson().firstName;
	}

	public String getLastName() {
		return getPerson().lastName;
	}

	public String getName() {
		return getFirstName() + " " + getLastName();

	}

	public String getZip() {
		return getPerson().zip;
	}

	public String getCity() {
		return getPerson().city;

	}
	public Long getId()
	{
		return getPerson().id;
	}

	public String getState() {
		return getPerson().state;
	}

	public String getEmail() {
		return getPerson().email;

	}

	public String getPhoneNumber() {
		return getPerson().phone;

	}

	public boolean getIsFriendOrYou() {
		return getIsFriend() || getIsYou();
	}

	public boolean getIsFriend() {
		if (((Person) request.getSession(true).getAttribute("user")).friendList == null) {
			return false;
		} else {
			for (Person ppp : getPerson().friendList) {
				if (ppp.id.equals(((Person) request.getSession(true)
						.getAttribute("user")).id)) {
					return true;
				}
			}
			return false;
		}
	}

	/**
	 * Gibt true zur�ck, wenn aktuelle Person der aktuelle Benutzer ist.
	 * @return
	 */
	public boolean getIsYou() {
		return getPerson()
				.equals(request.getSession(true).getAttribute("user"));
	}

	/**
	 * Gibt true zur�ck, wenn bereits eine Freundschaftsanfrage versendet wurde.
	 * @return
	 */
	public boolean getRequestSend() {
		if (getPerson().friendRequests == null) {
			return false;
		} else {
			for (Person ppp : getPerson().friendRequests) {
				if (ppp.id.equals(((Person) request.getSession(true)
						.getAttribute("user")).id)) {
					return true;
				}
			}
			return false;
		}
	}

	public String getGreet() {

		return null;

	}

	public List<Comments> getComments() {
		return currentMessage.getComments();
	}

	@Property
	@Persist
	private String message;

	public List<Messages> getMessages() {

		return getPerson().messageList;
	}

	public String getPhotoSource() {
		if (getPerson().photoSource != null) {
			return getPerson().photoSource;
		} else {
			return "layout/profilepictures/dummy.jpg";
		}
	}

	@CommitAfter
	void onActionFromSendRequest(Person p) {
		Person pp = ((Person) request.getSession(true).getAttribute("user"));
		if (p.friendRequests == null) {
			p.friendRequests = new ArrayList<Person>();
		}
		p.friendRequests.add(pp);
		session.update(p);
		try {
			response.sendRedirect("../profile?uid=" + p.id);
		} catch (IOException e) {
		}
	}
	
	@CommitAfter
	void onActionFromResetRequest(Person p) {
		LinkedList<Person> l = new LinkedList<Person>();
		
		for (Person ppp : p.friendRequests) {
			if (ppp.id.equals(((Person) request.getSession(true)
					.getAttribute("user")).id)) {
				l.add(ppp);
			}
		}
		for (Person ppp : l) {
			p.friendRequests.remove(ppp);
		}
		
		session.update(p);
		try {
			response.sendRedirect("../profile?uid=" + p.id);
		} catch (IOException e) {
		}
	}
	
	@CommitAfter
	void onActionFromQuitFriendship(Person p) {
		LinkedList<Person> l = new LinkedList<Person>();
		
		for (Person ppp : p.friendList) {
			if (ppp.id.equals(((Person) request.getSession(true)
					.getAttribute("user")).id)) {
				l.add(ppp);
			}
		}
		for (Person ppp : l) {
			p.friendList.remove(ppp);
		}
		l.clear();
		for (Person ppp : ((Person) request.getSession(true)
				.getAttribute("user")).friendList) {
			if (ppp.id.equals(p.id)) {
				l.add(ppp);
			}
		}
		for (Person ppp : l) {
			((Person) request.getSession(true)
					.getAttribute("user")).friendList.remove(ppp);
		}
		
		session.update(p);
		session.update(((Person) request.getSession(true)
				.getAttribute("user")));
		try {
			response.sendRedirect("../");
		} catch (IOException e) {
		}
	}

	@CommitAfter
	public void onSubmitFromCommentForm() {
		Person p = getPerson();
		currentMessage.pushComment(new Comments(comment, p));

		session.update(currentMessage);
	}
	public void onSubmitFromPrivateMessageForm() throws IOException{
		response.sendRedirect("privatemessages?uid=" + getId());
	}
}
